package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.Constants;
import com.sofwin.R;
import com.sofwin.pojo.SysUser;
import com.sofwin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @packageName: com.sofwin.controller
 * @user: andyliu
 * @date: 2023/5/12 11:31
 * @email 115176513@qq.com
 * @description: TODO
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("list")
    public String list(SysUser user, @RequestParam(required = true,defaultValue = "1") Integer pageNumber,
                       @RequestParam(required = true,defaultValue = Constants.PAGE_SIZE) Integer pageSize,
                       Model model){
        PageInfo<SysUser> page = userService.queryByPage(user, pageNumber, pageSize);
        model.addAttribute("page",page);
        model.addAttribute("user",user);
        return "user/list";
    }
    @GetMapping("data")
    @ResponseBody
    public R data(SysUser user,@RequestParam(required = true,defaultValue = "1") Integer pageNumber,
                  @RequestParam(required = true,defaultValue = Constants.PAGE_SIZE) Integer pageSize){
        PageInfo<SysUser> page = userService.queryByPage(user,pageNumber,pageSize);
        return R.success(page);
    }
    @GetMapping("toEdit")
    public String toEdit(Integer id,Model model){
        if(id!=null){
            SysUser user = userService.queryById(id);
            model.addAttribute("user",user);
        }
        return "user/edit";
    }
    @PostMapping("save")
    @ResponseBody
    public boolean save(SysUser user){
        boolean status = userService.saveOrUpdate(user);
        return status;
    }
    @DeleteMapping("delete/{id}")
    @ResponseBody
    public R delete(@PathVariable Integer id){
        boolean status = userService.removeById(id);
        return R.success(status);
    }
    @RequestMapping("deletes")
    @ResponseBody
    public R deletes(@RequestParam(name = "ids[]") Integer[] ids){
        boolean status = userService.removeById(ids);
        return R.success(status);
    }
}
